<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                :model="queryParams"
                class="mb-[-16px]"
                inline
            >
                <el-form-item class="w-[280px]" label="订单ID" prop="order_id">
                    <el-input v-model="queryParams.order_id" clearable placeholder="请输入订单ID" />
                </el-form-item>
                <el-form-item class="w-[280px]" label="反馈类型" prop="type">
                    <el-select v-model="queryParams.type" clearable placeholder="请选择">
                        <el-option :value="1" label="设备保修" />
                        <el-option :value="2" label="订单反馈" />
                        <el-option :value="3" label="合作加盟" />
                        <el-option :value="4" label="意见反馈" />
                    </el-select>
                </el-form-item>
                <el-form-item class="w-[280px]" label="反馈状态" prop="status">
                    <el-select v-model="queryParams.status" clearable placeholder="请选择">
                        <el-option :value="1" label="已发起" />
                        <el-option :value="2" label="已处理" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column label="反馈类型" prop="type"
                                     show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag v-if="row.type == 1">设备报修</el-tag>
                            <el-tag v-if="row.type == 2">订单反馈</el-tag>
                            <el-tag v-if="row.type == 3">合作加盟</el-tag>
                            <el-tag v-if="row.type == 4">意见反馈</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="订单ID" prop="order_id" show-overflow-tooltip />
                    <el-table-column label="设备ID" prop="device_id" show-overflow-tooltip />
                    <el-table-column label="内容" prop="content" show-overflow-tooltip />
                    <el-table-column label="反馈结果" prop="result" show-overflow-tooltip />
                    <el-table-column label="反馈状态" prop="status" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag v-if="row.status == 1">已发起</el-tag>
                            <el-tag v-if="row.status == 2">已处理</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="120">
                        <template #default="{ row }">
                            <el-button
                                v-if="row.status == 1"
                                v-perms="['boom_feedback/edit']"
                                link
                                type="primary"
                                @click="handleEdit(row)"
                            >
                                反馈
                            </el-button>
                            <el-button
                                v-perms="['boom_feedback/delete']"
                                link
                                type="danger"
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @close="showEdit = false" @success="getLists" />
    </div>
</template>

<script lang="ts" name="boomFeedbackLists" setup>
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiBoomFeedbackDelete, apiBoomFeedbackLists } from '@/api/boom_feedback'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    type: '',
    order_id: '',
    device_id: '',
    result: '',
    status: ''
})


// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiBoomFeedbackLists,
    params: queryParams
})


// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open()
    editRef.value?.getDetail(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiBoomFeedbackDelete({ id })
    getLists()
}

getLists()
</script>

